
cc.Class({
    extends: require('wy-Component'),

    properties: {
       layout:cc.Node,
       sup:[cc.Node],

       pageView : require('PageView')
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        this.unclocked=Math.floor(global.Data.currentPass);
        this.unclocked=Math.min(this.unclocked,9)
        var lastUseSup=parseInt(cc.sys.localStorage.getItem('usedSup'));
        for(let i=0;i<this.sup.length;i++){
            this.sup[i].children[0].active=false;
            cc.find('tips',this.sup[i]).getComponent(cc.Label).string='第'+(i)+'关解锁'
            cc.find('locked',this.sup[i]).active = (i>this.unclocked)
            cc.find('tips',this.sup[i]).active = (i>this.unclocked)
        }

        if(this.unclocked>=lastUseSup && lastUseSup>=0){
            this.sup[lastUseSup].children[0].active=true;
        }else{
            this.unclocked=this.unclocked>this.sup.length-1?this.sup.length-1:this.unclocked;
            this.unclocked>=0 && (this.sup[this.unclocked].children[0].active=true);
        }
        // this.onViewChange(null,global.supId)
        if(global.supId>=0){
            this.setCenter(global.supId);
        }
        
    },

    onViewChange(pv,idx){
        cc.log('idx:',idx)
        if(this.unclocked>=idx){
            this.changeBg(idx)
        }else{
            // global.EJMgr.showToast('通过第'+(idx*2+2)+'关解锁')
        }

        
        
    },

    start () {
    },


    changeBg(number){
        for(let i=0;i<this.sup.length;i++){
            this.sup[i].children[0].active=false;
        }
        cc.find('bg',this.sup[number]).active=true;
        global.Event.emit('showSup',{data:number})
    },

    setCenter(x){
        var lastUseSup=cc.sys.localStorage.getItem('usedSup');
        if(lastUseSup && this.unclocked>parseInt(lastUseSup)){
            this.layout.x=-this.layout.children[parseInt(lastUseSup)].x
            return
        }
        if(x>=0){
            this.layout.x=-this.layout.children[x].x;
        }else{
            this.layout.x=-80
        }
        
    },

    onTouch(event){
        if(event.type == cc.Node.EventType.TOUCH_START){
            this.lastPoint = event.touch.getStartLocation();
            return;
        }
        
        var current = event.touch.getLocation();
        var p = current.x-this.lastPoint.x;
        this.laoutMove(p);
        this.lastPoint=current;
    },

    laoutMove(p){
        if(this.layout.x>=-80 && p>0)return;
        if(this.layout.x<=-1730 && p<0)return;
        this.layout.x+=p;
    },
    update (dt) {
        if(this.layout.x>-80){
            this.layout.x=-80
        }
        if(this.layout.x<-1730){
            this.layout.x=-1730
        }
    },
});
